package com.qiwo.destop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.qiwo.common.domain.ContentDO;
import com.qiwo.common.service.ContentService;
import com.qiwo.common.utils.HttpContextUtils;
import com.qiwo.destop.product.GetProduct;
import com.qiwo.webshow.domain.*;
import com.qiwo.webshow.service.BannerService;
import com.qiwo.webshow.service.ColumnService;
import com.qiwo.webshow.service.SeriesintroService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Copyright
 *
 * @Author:chenwenchao
 * @Date:2018/10/26 0026
 * Description:
 */
public class BaseController {

    @Autowired
    private BannerService bannerService;

    protected BannerDO getBanner(Integer typeCode){
        Wrapper<BannerDO> bannerDOWrapper=new EntityWrapper<>();
        bannerDOWrapper.eq("typecode",typeCode).orderBy("sort",true);
        BannerDO bannerDO= bannerService.selectOne(bannerDOWrapper);
        return  bannerDO;
    }
    /**
     * <pre>
     * 自动获取分页参数，返回分页对象page
     * </pre>
     *
     * <small> 2018年4月15日 | Aron</small>
     *
     * @param e
     * @return
     */
    public <E> Page<E> getPage(Class<E> e) {
        int pageNumber = getParaToInt("pageNumber", 1);
        int pageSize = getParaToInt("pageSize", 10);
        return new Page<>(pageNumber, pageSize);
    }


    private int getParaToInt(String key, int defalut) {
        String pageNumber = HttpContextUtils.getHttpServletRequest().getParameter(key);
        if (StringUtils.isBlank(pageNumber)) {
            return defalut;
        }
        return Integer.parseInt(pageNumber);
    }

    @Autowired
    private SeriesintroService seriesintroService;
    @Autowired
    protected GetProduct getProduct;

    @Autowired
    private ColumnService columnService;

    @Autowired
    private ContentService contentService;

    protected Model returnDetail(Model model,Integer id){
        ProductDO productDO=getProduct.getProductDetail(id);
        SeriesintroDO seriesintroDO= seriesintroService.selectById(productDO.getSeriesIntroid());
        seriesintroDO=getProduct.getSeriesInfo(Integer.parseInt(seriesintroDO.getId().toString()));
        model.addAttribute("getSeriesInfo",seriesintroDO);
        model.addAttribute("product",productDO);
        model.addAttribute("seotitle",seriesintroDO.getSeriesName()+"-"+seriesintroDO.getName()+"-"+productDO.getProductName());
        model.addAttribute("seokeywords",seriesintroDO.getSeriesName()+"-"+seriesintroDO.getName()+"-"+productDO.getProductName());
        List<ProductImgDO> imgDOList=getProduct.getProductImg(id);
        String fristImg= productDO.getProductImg();
        if(imgDOList!=null&&imgDOList.size()>0){
            fristImg=imgDOList.stream().findFirst().get().getImageUrl();
            model.addAttribute("imgs",imgDOList);
        }else{
            ProductImgDO imgDO=new ProductImgDO();
            imgDO.setImageUrl(fristImg);
            imgDOList=new ArrayList<>();
            imgDOList.add(imgDO);
            model.addAttribute("imgs",imgDO);
        }
        model.addAttribute("fristImg",fristImg);
        return  model;
    }

    protected ColumnMenuDO getColumnDo(Integer id){
        Wrapper<ColumnMenuDO> columnMenuDOWrapper=new EntityWrapper<>();
        columnMenuDOWrapper.eq("id",id);
        ColumnMenuDO columnMenuDO=columnService.selectOne(columnMenuDOWrapper);
        return  columnMenuDO;
    }
    protected ContentDO getContentDo(Integer contentId){
        Wrapper<ContentDO> contentDOWrapper=new EntityWrapper<>();
        contentDOWrapper.eq("type_id",contentId);
        ContentDO contentDO=contentService.selectOne(contentDOWrapper);
        return contentDO;
    }

    protected  void setBaseModel(Model model){
        Map<String,Object> map=(Map<String,Object>)model.asMap().get("concurrentModel");
        model.addAttribute("copyright",map.get("copyright"));
        model.addAttribute("seotitle",map.get("seotitle"));
        model.addAttribute("seokeywords",map.get("seokeywords"));
        model.addAttribute("golbalTel",map.get("golbalTel"));
        model.addAttribute("qq",map.get("qq"));
        model.addAttribute("WeiChatImg",map.get("WeiChatImg"));
        model.addAttribute("weiboAddress",map.get("weiboAddress"));
        model.addAttribute("rigthtTel",map.get("rigthtTel"));
        model.addAttribute("latitude",map.get("latitude"));
        model.addAttribute("longitude",map.get("longitude"));
        model.addAttribute("companyName",map.get("companyName"));
        model.addAttribute("companyAddress",map.get("companyAddress"));
    }
}
